package test;

import java.nio.charset.Charset;
import java.security.Security;

import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.junit.Test;

import indi.h.security.encryption.AsymCrypt;
import indi.h.security.encryption.Crypt;
import indi.h.security.encryption.CryptFactory;
import indi.h.security.encryption.asym.AsymCryptFactory;
import indi.h.security.encryption.asym.IESCrypt;
import indi.h.security.encryption.asym.IESCryptParameter;
import indi.h.security.encryption.asym.IESCryptType;
import indi.h.security.encryption.asym.KeyAgrCryptImp;
import indi.h.security.encryption.asym.KeyAgrCryptParameter;
import indi.h.security.encryption.asym.KeyAgrCryptType;
import indi.h.security.encryption.asym.KeyAgrMode;

public class KeyAgrTest2 {
	public static void main(String[] args) {
		Security.addProvider(new BouncyCastleProvider());
		CryptFactory cf = AsymCryptFactory.getInstance(KeyAgrCryptType.ECIES);
		
		KeyAgrCryptImp c1 = (KeyAgrCryptImp) cf.createCrypt();

		KeyAgrCryptParameter scp = new KeyAgrCryptParameter();
		scp.setAlgorithm(c1.getAlgorithm());
		scp.setCipherAlgorithm(c1.getCipherAlgorithm());
		scp.setCode(Charset.forName("utf-8"));
		scp.setLength(c1.getLength());
		scp.setPublicKey(((KeyAgrCryptImp) c1).getPublicKey());
		scp.setKeyAgrAlgorithm(c1.getKeyAgrAlgorithm());
		scp.setKeyAgrMode(KeyAgrMode.MQV);
		scp.setPassword("sbx");
		scp.setKeyFactory("EC");

		Crypt c2 = cf.createCrypt(scp);
		
		((AsymCrypt) c1).putUpPublicKey(((AsymCrypt) c2).getPublicKey());

		byte[] encrypt2 = c1.encrypt("sb".getBytes());
		byte[] encrypt3 = c1.encrypt("sb2".getBytes());
		byte[] encrypt4 = c1.encrypt("sb3".getBytes());
		byte[] encrypt5 = c1.encrypt("sb5".getBytes());
		byte[] encrypt = c2.encrypt("zzzz".getBytes());

		System.out.println(new String(encrypt));
		System.out.println(new String(encrypt2));

		System.out.println("xxxxxxxxxxxxxxxxxxxxxx");

		System.out.println(new String(c1.decrypt(encrypt)));
		System.out.println(new String(c2.decrypt(encrypt5)));
	}

	@Test
	public void testIES() {
		Security.addProvider(new BouncyCastleProvider());
		CryptFactory cf = AsymCryptFactory.getInstance(IESCryptType.EC);
		IESCrypt c1 = (IESCrypt) cf.createCrypt();

		IESCryptParameter scp = new IESCryptParameter();
		scp.setAlgorithm(c1.getAlgorithm());
		scp.setCipherAlgorithm(c1.getCipherAlgorithm());
		scp.setCode(Charset.forName("utf-8"));
		scp.setLength(c1.getLength());
		scp.setPublicKey(((IESCrypt) c1).getPublicKey());
		scp.setPassword("sbx");
		scp.setKeyFactory(IESCryptType.EC.toString());

		IESCrypt c2 = (IESCrypt) cf.createCrypt(scp);
		c1.putUpPublicKey(c2.getPublicKey());

		byte[] encrypt2 = c1.encrypt("sb".getBytes());
		byte[] encrypt3 = c1.encrypt("sb2".getBytes());
		byte[] encrypt4 = c1.encrypt("sb3".getBytes());
		byte[] encrypt5 = c1.encrypt("sb5".getBytes());
		byte[] encrypt = c2.encrypt("zzzz".getBytes());

		System.out.println(new String(encrypt));
		System.out.println(new String(encrypt2));

		System.out.println("xxxxxxxxxxxxxxxxxxxxxx");
		c1.setIES(c2.getIES());
		c2.setIES(c1.getIES());

		System.out.println(new String(c1.decrypt(encrypt)));
		System.out.println(new String(c2.decrypt(encrypt5)));
	}
}
